package com.bsy.common.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 * Created by Sincerity on 2024/6/5.
 * Copyright
 * 描述：
 */
class ViewPager2RecyclerView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr) {


    // Override onTouchEvent method
    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (canRecyclerViewScrollHorizontally(event)) {
            // Disallow ViewPager2 to intercept touch events
            parent.requestDisallowInterceptTouchEvent(true)
            return true // RecyclerView handles touch event
        }
        return super.onTouchEvent(event) // ViewPager2 handles it
    }

    // Implement canRecyclerViewScrollHorizontally helper function
    private fun canRecyclerViewScrollHorizontally(event: MotionEvent): Boolean {
        val layoutManager = layoutManager
        if (layoutManager is LinearLayoutManager) {
            val orientation = layoutManager.orientation
            return if (orientation == LinearLayoutManager.HORIZONTAL) {
                layoutManager.canScrollHorizontally() // Check for right-side scrolling
            } else {
                false
            }
        }
        return false
    }
}